﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Globalization;

namespace weblog
{
    public partial class treeviwtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                DataView dv = drv("select * from HouseOfBuild");
                foreach (DataRowView datav in dv)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = datav["BuildName"].ToString();//Big_Nav 为数据库中字段
                    tn.Value = datav["BuildID"].ToString();//Big_Id 为数据库中字段
                    tn.Expanded = false;
                    //tn.NavigateUrl = "~/user/list.aspx?check=" + datav["Big_Id"].ToString();//点击导航中值的跳转 页面
                    //tn.Target = "rightFrame";//因为我用的是框架，所以有这个设置，让它在右框架中显示页面
                    TreeView1.Nodes.Add(tn);
                    
                    addchildnode(tn);

                    TreeView1.Width = 150;
                    DropDownList_area.Width = 160;
                    GridView_ClientOfHouse2Client.Width = 150;
                }
            }
        }

        private void addchildnode(TreeNode tn)
        {
           // string bid = Convert.ToInt32(tn.Value);
            string bid = Convert.ToString(tn.Value);
            DataView dv = drv("select * from HouseOfFloor where BuildID=" + bid + "");
            foreach (DataRowView datav in dv)
            {
                TreeNode ctn = new TreeNode();
                ctn.Text = datav["FloorName"].ToString();
                ctn.Value = datav["FloorID"].ToString();
                //ctn.NavigateUrl = "~/user/list.aspx?check=" + datav["BuildID"].ToString();
                //ctn.Target = "rightFrame";

                //ctn.SelectAction=none;
                addchildsmallnode(ctn);
                tn.ChildNodes.Add(ctn);
            }


        }

        private void addchildsmallnode(TreeNode utn)
        {
            // string bid = Convert.ToInt32(tn.Value);
            string bid = Convert.ToString(utn.Value);
            DataView dv = drv("select * from HouseOfHouse where FloorID=" + bid + "");
            foreach (DataRowView datav in dv)
            {
                TreeNode cstn = new TreeNode();
                cstn.Text = datav["HouseName"].ToString();
                cstn.Value = datav["HouseID"].ToString();
                //ctn.NavigateUrl = "~/user/list.aspx?check=" + datav["BuildID"].ToString();
                //ctn.Target = "rightFrame";

                //ctn.SelectAction=none;
                utn.ChildNodes.Add(cstn);
            }


        }


        private DataView drv(string query)
        {
            //string connstr = "Persist Security Info=false;Integrated Security=true;database=keyboli;Server=.\\sqlexpress";
            //SqlConnection conn = new SqlConnection(connstr);
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fytpmsDemoConnectionString"].ConnectionString);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "tree");
            return ds.Tables["tree"].DefaultView;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
